package com.rd.student.ruanjian.class4.zhangrui.day7_31;

import java.util.*;

public class RandomNum {
    public static void main(String[] args) {
        //  Num1(20);
       // Num2(20);
        //Num3(20);
        //Num4(20);
       // Num5(20);
    }

    public static List<Integer> Num1(int n) {
        Random r = new Random();
        List<Integer> listnum = new ArrayList<>();
        while (n > listnum.size()) {
            int num = r.nextInt(50);
            if (!listnum.contains(num)) {
                listnum.add(num);
            }
        }

        for (int i : listnum) {
            System.out.println(i);
        }
        return listnum;
    }

    //-------------------------------------------------------------
    public static int[] Num2(int n) {
        int[] indexArr = new int[n];
        int cut = 0;
        for (int i = 0; i < n; i++) {
            int index = (int) (Math.random() * 50);

            boolean flag = false;
            for (int j = 0; j < cut; j++) {
                if (index == indexArr[j]) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                indexArr[cut] = index;
                cut++;
            } else {
                i--;
                continue;
            }
        }
        System.out.println(Arrays.toString(indexArr));
        return indexArr;
    }
    //----------------------------------------------------

    public static int[] Num3(int n){
        int[] indexArr = new int[n];
        for (int i = 0; i < indexArr.length; i++) {
            indexArr[i]=-1;
        }
        for (int i = 0; i < n; i++) {
            int random=(int)(Math.random()*20);
            if (indexArr[random]==-1){
               int random1=(int)(Math.random()*50);
                indexArr[random]=random1;
            }else {
                i--;
            }
        }
        System.out.println(Arrays.toString(indexArr));
        return indexArr;
    }
    //---------------------------------------------------

    public static HashSet<Integer> Num4(int n){
        Random r=new Random();

        HashSet<Integer> set=new HashSet<>();
        while(n>set.size()){
            int random=r.nextInt(50);
            set.add(random);
        }
        System.out.println(set);
        return set;
    }
    //------------------------------------------------
    public static void Num5(int n){
        ArrayList<Integer> numlist=new ArrayList<>(n);
        Random r=new Random();

        for (int i = 0; i < n; i++) {
            int random=r.nextInt(50);
            numlist.add(random);
        }
        Collections.shuffle(numlist);
        for (int i = 0; i < numlist.size(); i++) {
            System.out.println(numlist.get(i));
        }
    }
    //-------------------------------------------------


}